//
//  humanViewController.m
//  theCoupleLive
//
//  Created by USER on 2016/12/5.
//  Copyright © 2016年 USER. All rights reserved.
//

#import "humanViewController.h"
#import "coupleLiveAPI.h"
#import "hunmanTableViewCell.h"
#import "timeModel.h"
#import "AppDelegate.h"
#define APPDELEGATE ((AppDelegate*)[[UIApplication sharedApplication] delegate])
@interface humanViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)NSMutableArray *humanArr;
@end

@implementation humanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.automaticallyAdjustsScrollViewInsets=NO;
    self.view.backgroundColor=[UIColor whiteColor];
    self.tableview=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49-64)];
    self.tableview.delegate=self;
    self.tableview.dataSource=self;
    [self.view addSubview:self.tableview];
    self.tableview.tableFooterView = [UIView new];
    self.humanArr=[[NSMutableArray alloc]init];
    [coupleLiveAPI nearbyPeopleWithuid:APPDELEGATE.user.userid ugps:@"31.980942,120.9195" success:^(id response) {
//        NSLog(@"%@",APPDELEGATE.user.userid);
//        NSLog(@"---%@",response);
        self.humanArr=response;
        [self.tableview reloadData];
    }];
    // Do any additional setup after loading the view.
}

#pragma mark section
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

#pragma mark row
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.humanArr.count;
}

#pragma mark cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    hunmanTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cellid"];
    if (cell==nil) {
        cell=[[[NSBundle mainBundle]loadNibNamed:@"hunmanTableViewCell" owner:nil options:nil]firstObject];
    }
    //头像
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData *decodedImageData   = [[NSData alloc] initWithBase64Encoding:[[self.humanArr objectAtIndex:indexPath.row]objectForKey:@"p_img"]];
        UIImage *decodedImage      = [UIImage imageWithData:decodedImageData];
        dispatch_async(dispatch_get_main_queue(), ^{
//            NSLog(@"%@",decodedImageData);
//            NSLog(@"%@",decodedImage);
            cell.headImage.image=decodedImage;
//            [self.tableview reloadData];
        });
    });
    //姓名
    cell.name.text=[[self.humanArr objectAtIndex:indexPath.row]objectForKey:@"p_nickname"];
    //性别
    if ([[[self.humanArr objectAtIndex:indexPath.row]objectForKey:@"p_sex"]isEqualToString:@"男"]) {
        cell.bView.backgroundColor=[UIColor colorWithRed:60.0/255 green:189.0/255 blue:252.0/255 alpha:1];
        cell.sexImg.image=[UIImage imageNamed:@"男"];
    }
    else{
        cell.bView.backgroundColor=[UIColor colorWithRed:252.0/255 green:72.0/255 blue:113.0/255 alpha:1];
        cell.sexImg.image=[UIImage imageNamed:@"女"];
    }
    //描述
    cell.desc.text=[[self.humanArr objectAtIndex:indexPath.row]objectForKey:@"p_explain"];
    //年龄
    cell.agelabel.text=[[self.humanArr objectAtIndex:indexPath.row]objectForKey:@"p_age"];
    //距离
    NSLog(@"%@",[NSString stringWithFormat:@"%@",[[self.humanArr objectAtIndex:indexPath.row]objectForKey:@"km"]]);
    float m=[[NSString stringWithFormat:@"%@",[[self.humanArr objectAtIndex:indexPath.row]objectForKey:@"km"]] floatValue];
    cell.distancelabel.text=[NSString stringWithFormat:@"%.2fkm",m/1000];
    //时间
    cell.timelabel.text=[timeModel timeAgoWithtime:[NSString stringWithFormat:@"%@",[[self.humanArr objectAtIndex:indexPath.row]objectForKey:@"signNow"]]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
